package com.guohao.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guohao.reggie.common.CustomException;
import com.guohao.reggie.dto.SetmealDto;
import com.guohao.reggie.entity.Setmeal;
import com.guohao.reggie.entity.SetmealDish;
import com.guohao.reggie.mapper.SetmealMapper;
import com.guohao.reggie.service.SetmealDishService;
import com.guohao.reggie.service.SetmealService;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐 同时保存 套餐和菜品关联关系
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //1 保存套餐的基本关联信息 执行setmeal 执行insert
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());

            Long setmealId = item.getSetmealId();
            System.out.println("setmealId = " + setmealId);
            return item;
        }).collect(Collectors.toList());


        //2. 保存套餐和菜品关联信息, 操作setmeal_dish 执行insert操作

        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 根据id 删除套餐 和 关联的菜品
     * @param ids
     */
    @Transactional
    @Override
    public void remove(List<Long> ids) {
        // 1. 查询套餐的状态, 确定是否可以删除
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.eq(Setmeal::getStatus,1);
        int count = this.count(lqw);

        if (count > 0){
            //售卖中 不能删除
            throw new CustomException("套餐正在售卖中,不能删除");
        }

        // 2. 如果可以删除, 先删除套餐中的数据
        this.removeByIds(ids);

        // 3. 删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();;
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(setmealDishLambdaQueryWrapper);

    }

    /**
     * 根据id查询 套餐和 对象的  菜品关联关系
     * @param id
     * @return
     */
    @Transactional
    @Override
    public SetmealDto getByIdWithSetmeal(Long id) {
        // 查询setmeal表中的信息
        Setmeal setmeal = this.getById(id);
        // 创建 SetmealDto 对象
        SetmealDto setmealDto = new SetmealDto();
        //拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询 setmeal_Dish表

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 更新 菜品信息 和  对应 的分类信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        // 更新 setmeal 表
        this.updateById(setmealDto);

        // 清理当前菜品对应口味数据 -- setmeal表的delete操作
        LambdaQueryWrapper<SetmealDish> setmealDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDtoLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(setmealDtoLambdaQueryWrapper);

        // 添加当前提交过来的口味数据 -- setmeal_dish表的insert操作

        List<SetmealDish> list = setmealDto.getSetmealDishes();

        list = list.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

       setmealDishService.saveBatch(list);

    }
}
